package top.kikt.book.core.handlers

import org.jsoup.nodes.Document
import top.kikt.book.core.entity.BookMetaEntity
import top.kikt.book.core.entity.ChapterEntity
import top.kikt.book.core.entity.ChapterMetaEntity
import top.kikt.book.core.handlers.common.BasePagingBookHandler
import top.kikt.book.core.handlers.common.CommonBookHandler
import top.kikt.book.core.handlers.common.CommonHandler
import top.kikt.book.core.handlers.common.CommonMenuHandler
import top.kikt.book.util.getUrl

class X810book : BasePagingBookHandler() {

    override val host: String
        get() = "www.x810book.com"

    override fun parseBookMeta(document: Document, url: String): BookMetaEntity {
        return CommonBookHandler.parseBookMeta(document, url)
    }

    override fun parsePagingChapterMeta(document: Document, menuUrl: String): List<ChapterMetaEntity> {
        val elements = document.getElementsByClass("section-box")[1]
        return CommonMenuHandler.convertToMetaList(elements, menuUrl)
    }

    override fun nextMenuPageUrl(document: Document, newMenuUrl: String): String? {
        val right = document.getElementsByClass("right")[0]

        val elements = right.getElementsByClass("onclick")
        return if (elements.isNotEmpty()) {
            elements[0].getUrl(newMenuUrl)
        } else {
            null
        }
    }

    override fun parseContent(document: Document): String {
        return CommonHandler.parseContent(document) ?: return ""
    }

    override fun nextContentPageUrl(document: Document, url: String): String? {
        val next = document.getElementById("next_url") ?: return null

        if (next.text().trim() != "下一页") {
            return null
        }

        return next.getUrl(url)
    }

}